package com.jxfcls.wnd_android;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.res.Resources;
import android.graphics.Paint;
import android.location.Location;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
	
	private ViewPager mViewPager;
	private PagerAdapter mPagerAdapter;
	private String[] titles;
	private List<TextView> textViews;
	private List<TextView> lineViews;
	private HorizontalScrollView scrollbar;
	private TextView login;
	private float windowHeight;
	private float windowWidth;
	private static int x = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		beforeInitView();
		initView();
	}

	
	/*
	 * 初始化数据
	 */
	private void beforeInitView() {
		//获取屏幕宽高
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		windowHeight = display.getHeight();
		windowWidth = display.getWidth();
		
		Resources res = getResources();
		titles = res.getStringArray(R.array.titles);
		
		textViews = new ArrayList<TextView>();
		textViews.add((TextView)findViewById(R.id.textView_button01));
		textViews.add((TextView)findViewById(R.id.textView_button02));
		textViews.add((TextView)findViewById(R.id.textView_button03));
		textViews.add((TextView)findViewById(R.id.textView_button04));
		textViews.add((TextView)findViewById(R.id.textView_button05));
		textViews.add((TextView)findViewById(R.id.textView_button06));
		textViews.add((TextView)findViewById(R.id.textView_button07));
		textViews.add((TextView)findViewById(R.id.textView_button08));
		textViews.add((TextView)findViewById(R.id.textView_button09));
		
		lineViews = new ArrayList<TextView>();
		lineViews.add((TextView)findViewById(R.id.tabbar_line01));
		lineViews.add((TextView)findViewById(R.id.tabbar_line02));
		lineViews.add((TextView)findViewById(R.id.tabbar_line03));
		lineViews.add((TextView)findViewById(R.id.tabbar_line04));
		lineViews.add((TextView)findViewById(R.id.tabbar_line05));
		lineViews.add((TextView)findViewById(R.id.tabbar_line06));
		lineViews.add((TextView)findViewById(R.id.tabbar_line07));
		lineViews.add((TextView)findViewById(R.id.tabbar_line08));
		lineViews.add((TextView)findViewById(R.id.tabbar_line09));
		
		int i= 0;
		for (TextView textView: textViews) {
			textView.setText(titles[i]);
			i++;
		}
		
	}
	
	/*
	 * 初始化控件
	*/
	private void initView() {
		
		mViewPager = (ViewPager)findViewById(R.id.viewPager);
		login = (TextView)findViewById(R.id.login_textView);
		scrollbar = (HorizontalScrollView)findViewById(R.id.tabbar_scrollLayout);
		mPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setOnPageChangeListener(mPageChangeListener);
		((TextView)textViews.get(0)).setSelected(true);
		
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)scrollbar.getLayoutParams();
		linearParams.width = (int)(windowWidth * 0.8);
		scrollbar.setLayoutParams(linearParams);
		
	}
	
	public void mTabClickListener(View view) {

		for (int i = 0; i < titles.length;i++) {
			((TextView)textViews.get(i)).setSelected(false);
			((TextView)lineViews.get(i)).setVisibility(View.GONE);
			if (view == textViews.get(i)) {
				((TextView)view).setSelected(true);				
				((TextView)lineViews.get(i)).setVisibility(View.VISIBLE);
				mViewPager.setCurrentItem(i);
			}
		}
	}
	
	public OnPageChangeListener mPageChangeListener = new OnPageChangeListener() {
		
		@Override
		public void onPageSelected(int arg0) {
			for (int i = 0; i < titles.length;i++) {
				((TextView)textViews.get(i)).setSelected(false);
				((TextView)lineViews.get(i)).setVisibility(View.GONE);
			}
			((TextView)textViews.get(arg0)).setSelected(true);
			((TextView)lineViews.get(arg0)).setVisibility(View.VISIBLE);

			//Tabbar的坐标
			int[] postion = new int[2];
			((TextView)textViews.get(arg0)).getLocationOnScreen(postion);
			//Login Button的坐标
			int[] location = new int[2];
			login.getLocationOnScreen(location);

			if (postion[0] >= location[0] - 30) {
				x += location[0];
				scrollbar.scrollTo((int)x, 0);
			}else if (postion[0] < 0) {
				x -= location[0];
				scrollbar.scrollTo((int)x, 0);
			}
			
		}
		
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			
		}
		
		@Override
		public void onPageScrollStateChanged(int arg0) {
			
		}
	};
	
	private class CollectionPagerAdapter extends FragmentStatePagerAdapter {

		public CollectionPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return CollectionFragment.create(titles[arg0]);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return titles.length;
		} 
		
	}
	
	public static class CollectionFragment extends Fragment {
		public static CollectionFragment create(String title) {
			CollectionFragment fragment = new CollectionFragment();
			Bundle bundle = new Bundle();
			bundle.putString("title", title);
			fragment.setArguments(bundle);
			return fragment;
		}

		@Override
		public View onCreateView(LayoutInflater inflater,
				@Nullable ViewGroup container,
				@Nullable Bundle savedInstanceState) {
			Random r = new Random(System.currentTimeMillis());
			Bundle b = getArguments();
			
			View v = inflater.inflate(R.layout.fragment_viewpaper_layout, null);
			v.setBackgroundColor(r.nextInt() >> 8 | 0xFF << 24);
			
			TextView txvAddress = (TextView)v.findViewById(R.id.textView);
			txvAddress.setTextColor(r.nextInt()>>8 | 0xFF << 24);
			txvAddress.setBackgroundColor(r.nextInt() >> 8 | 0xFF << 24);
			txvAddress.setText(b.getString("title",""));
			
			return v;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
